package com.ibeeking.found.cms.b.rest.controller.v1;

import cn.hutool.json.JSONUtil;
import com.ibeeking.found.cms.b.rest.service.IPageBaseService;
import com.ibeeking.found.cms.b.rest.service.IPageComponentsService;
import com.ibeeking.found.cms.b.rest.service.IPageModuleService;
import com.ibeeking.found.cms.service.common.constants.enums.PageCategoryEnum;
import com.ibeeking.found.cms.service.common.param.PageModuleAllParam;
import com.ibeeking.found.cms.service.common.query.*;
import com.ibeeking.found.cms.service.common.vo.*;
import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.db.annotation.Read;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.SystemEnum;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ibeeking.found.cms.service.common.query.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName PageBaseController控制器
 * @Description
 * @Author ibeeking
 * @Date 2021-05-08 16:05
 **/
@RestController
@RequestMapping("/b/page/v1")
public class PageController {

    @Resource
    private IPageComponentsService pageComponentsServiceImpl;

    @Resource
    private IPageBaseService pageBaseServiceImpl;

    @Resource
    private IPageModuleService pageModuleServiceImpl;

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看所有页面组件")
    @PostMapping(value = "/pageComponents/queryAllComponents")
    public List<PageComponentsVO> queryAllComponents() {
        return pageComponentsServiceImpl.queryAll();
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "自定义页面分页")
    @PostMapping(value = "/pageBase/customPageList")
    public Page<PageBaseVO> customPageList(@RequestBody PageBasePageQuery pageBasePageQuery) {
        pageBasePageQuery.setPlatform(SystemEnum.TENANT.getValue());
        pageBasePageQuery.setCategory(PageCategoryEnum.CUSTOM.getValue());
        return pageBaseServiceImpl.pageList(pageBasePageQuery);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据页面类型查询有效页面分页")
    @PostMapping(value = "/pageBase/effectiveByTypePageList")
    public Page<EffectivePageVO> effectiveByTypePageList(@RequestBody EffectiveByTypePageQuery query) {
        return pageBaseServiceImpl.effectiveByTypePageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "保存页面")
    @PostMapping(value = "/pageModule/save")
    public PageModuleAllView save(@RequestBody String paramJson, @LoginUser CurrentUser currentUser) {
        PageModuleAllParam param = JSONUtil.toBean(paramJson, PageModuleAllParam.class);
        return pageModuleServiceImpl.save(param, currentUser);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "")
    @PostMapping(value = "/pageModule/publish/{id}")
    public Boolean publish(@PathVariable("id") Long id) {
        return pageModuleServiceImpl.publish(id);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面(根据页面id)")
    @PostMapping(value = "/pageModule/viewPage/{pageId}")
    public PageModuleAllView viewPage(@PathVariable("pageId") Long pageId) {
        return pageModuleServiceImpl.viewByPageId(pageId);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看商城页面")
    @PostMapping(value = "/pageModule/viewPage/mallHome")
    public PageModuleAllView viewPageMallHome() {
        return pageModuleServiceImpl.viewByPageCategory(PageCategoryEnum.MALL_HOME);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面(根据页面类别)")
    @PostMapping(value = "/pageModule/viewPageByCategory/{pageCategory}")
    public PageModuleAllView viewPageMallHome(@PathVariable("pageCategory") Integer pageCategory) {
        return pageModuleServiceImpl.viewByPageCategory(PageCategoryEnum.getEnum(pageCategory));
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "")
    @PostMapping(value = "/pageBase/shopPageList")
    public Page<PageBaseVO> shopPageList(@RequestBody PageBasePageQuery pageBasePageQuery) {
        pageBasePageQuery.setPlatform(SystemEnum.SHOP.getValue());
        return pageBaseServiceImpl.pageList(pageBasePageQuery);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "")
    @PostMapping(value = "/pageBase/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return pageBaseServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "")
    @PostMapping(value = "/pageBase/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return pageBaseServiceImpl.del(id);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面组件内容(查看页面时调用)")
    @PostMapping(value = "/pageModule/pageModuleData/{id}")
    public PageModuleView pageModuleData(@PathVariable("id") Long id) {
        return pageModuleServiceImpl.pageModuleData(id);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面组件内容(根据组件id 及其他条件)")
    @PostMapping(value = "/pageModule/pageModuleData")
    public PageModuleView pageModuleData(@RequestBody @Validated PageModuleDataQuery query) {
        return pageModuleServiceImpl.pageModuleData(query);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面组件内容(选择组件内容时调用)")
    @PostMapping(value = "/pageModule/pageModuleDataSelQuery")
    public PageModuleView pageModuleDataSelQuery(@RequestBody PageModuleDataSelQuery query) {
        return pageModuleServiceImpl.pageModuleDataSelQuery(query);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面组件子内容(根据组件id,子元素id)")
    @PostMapping(value = "/pageModule/pageModuleChildData")
    public List pageModuleChildData(@RequestBody @Validated PageModuleChildDataQuery query) {
        return pageModuleServiceImpl.pageModuleChildData(query);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面组件子内容(选择组件内容时调用)")
    @PostMapping(value = "/pageModule/pageModuleChildDataSelQuery")
    public List pageModuleChildDataSelQuery(@RequestBody @Validated PageModuleChildDataSelQuery query) {
        return pageModuleServiceImpl.pageModuleChildDataSelQuery(query);
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查看页面组件子内容分页数据")
    @PostMapping(value = "/pageModule/pageModuleChildDataPageList")
    public Page pageModuleChildDataPageList(@RequestBody @Validated PageModuleChildDataPageListQuery query) {
        return pageModuleServiceImpl.pageModuleChildDataPageList(query);
    }


}